project(bug335803)

function(extract_single_arg1 _prefix _arg)
  set(_result)
  if("${ARGV2}" STREQUAL "${_arg}")
    set(_result ${ARGV3})
  endif()
  set(${_prefix}_TARGET_NAME ${_result} PARENT_SCOPE)
endfunction()

function(echo _arg)
  extract_single_arg1(ECHO "TARGET_NAME" ${ARGN})
  if(ECHO_TARGET_NAME)
    set(_target ${ECHO_TARGET_NAME})
  else()
    set(_target ${_arg})
  endif()
  message(STATUS "adding target echo-${_target}")
  add_custom_target(echo-${_target} ALL VERBATIM COMMAND "echo" "${_arg}")
endfunction()

echo(a)
echo(b)
echo(c TARGET_NAME custom_name)
echo(d) # ERROR: kdevelop tries to create target echo-custom_name again, instead of echo-d!

function(extract_single_arg2 _prefix _arg)
  set(${_prefix}_${_arg})
  if("${ARGV2}" STREQUAL "${_arg}")
    set(${_prefix}_${_arg} ${ARGV3})
  endif()
  set(${_prefix}_${_arg} ${${_prefix}_${_arg}} PARENT_SCOPE)
endfunction()

function(echo2 _arg)
  extract_single_arg2(ECHO2 "TARGET_NAME" ${ARGN})
  if(ECHO2_TARGET_NAME)
    set(_target ${ECHO2_TARGET_NAME})
  else()
    set(_target ${_arg})
  endif()
  message(STATUS "adding target echo2-${_target}")
  add_custom_target(echo2-${_target} ALL VERBATIM COMMAND "echo" "${_arg}")
endfunction()

echo2(a)
echo2(b)
echo2(c TARGET_NAME custom_name)
echo2(d) # ERROR: kdevelop tries to create target echo-custom_name again, instead of echo-d!
